<?php if(!isset($_SESSION)){
  session_start(); $message="";
}
	require_once('../../../Connections/microassur.php');
	require_once('../../utils/fonctions.php');
 ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$bool=false;

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){
 $requete=mysql_query("select * from garantiesouscrite where Identifiant_garantie='".$_POST['Identifiant_garantie']."' and Identifiant_contrat='".$_SESSION['idcontrat']."' and Identifiant_personne='".$_SESSION['idpers']."'")or die(mysql_error());
 if($requete)
 { 	
 	$num=mysql_num_rows($requete);
 	if($num>0)$bool=true;
 }

}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && !$bool) {
$req=mysql_query("select * from contrat where Numero_contrat='".$_SESSION['num_contrat']."'");
$dateeffet=-1;
if($req && mysql_num_rows($req)){
$rep=mysql_fetch_array($req);
$dateeffet=$rep['Date_effet_contrat'];
}
echo $dateeffet;
  $insertSQL = sprintf("INSERT INTO garantiesouscrite (Identifiant_garantie_souscrite, Etat_garantie_souscrite, Date_effet_garantie_souscrite, Date_fin_effet_garantie_souscrite, Identifiant_contrat, Identifiant_personne, Identifiant_garantie, Identifiant_formule) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_garantie_souscrite'], "int"),
                       GetSQLValueString("A", "text"),
                       GetSQLValueString($dateeffet, "date"),
                       GetSQLValueString("", "date"),
                       GetSQLValueString($_SESSION['idcontrat'], "int"),
                       GetSQLValueString($_SESSION['idpers'], "int"),
                       GetSQLValueString($_POST['Identifiant_garantie'], "int"),
                       GetSQLValueString($_SESSION['formule'], "int"));

  mysql_select_db($database_microassur, $microassur);
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  if(!Result1)$message.="<br>Problème création garantie souscrite optionnelle, contacter la DSI";
  else
	 $message=colorer("La Garantie souscrite optionnelle a été modifiée","true");
}
elseif((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $bool)$message=colorer("La garantie optionnelle existe déjà","false");

$colname_rs_pers = "-1";
if (isset($_GET['Identifiant_personne'])) {
  $colname_rs_pers = $_GET['Identifiant_personne'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_pers = sprintf("SELECT * FROM personne WHERE Identifiant_personne = %s", GetSQLValueString($colname_rs_pers, "int"));
$rs_pers = mysql_query($query_rs_pers, $microassur) or die(mysql_error());
$row_rs_pers = mysql_fetch_assoc($rs_pers);
$totalRows_rs_pers = mysql_num_rows($rs_pers);

mysql_select_db($database_microassur, $microassur);
$query_rsgarantie = "SELECT * FROM garantie";
$rsgarantie = mysql_query($query_rsgarantie, $microassur) or die(mysql_error());
$row_rsgarantie = mysql_fetch_assoc($rsgarantie);
$totalRows_rsgarantie = mysql_num_rows($rsgarantie);
 if(!isset($_SESSION)){
  session_start(); $message="";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modification administrative des éléments du contrat</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
</head>

<body>
			<div id="sf_menu1">
				<h3>Production</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Modadmin";
					$nommodule2="Liste Modadmin";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
	<div class="corps1">
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<!--/***************************************/-->
	<div class="imgleft"></div>
	<div class="imgmiddle">
		<div class="btn">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nommodule; ?></h5>
            </span><span class="btnright"></span>
        </div>
		<div class="btn1">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nommodule2; ?></h5>
            </span><span class="btnright"></span>
        </div>
		<div class="cleared"></div>
  <table class="gr">
    <tr>
      <td>GARANTIE OPTIONNELLE:</td>
      <td><select name="Identifiant_garantie">
        <?php
do {  
?>
        <option value="<?php echo $row_rsgarantie['Identifiant_garantie']?>"><?php echo $row_rsgarantie['Libelle_garantie']?></option>
        <?php
} while ($row_rsgarantie = mysql_fetch_assoc($rsgarantie));
  $rows = mysql_num_rows($rsgarantie);
  if($rows > 0) {
      mysql_data_seek($rsgarantie, 0);
	  $row_rsgarantie = mysql_fetch_assoc($rsgarantie);
  }
?>
      </select>
      </td>
	  </tr>
  </table>
		<div class="bt1"><a href="../index.php?Numero_contrat=<?php echo $_SESSION['num_contrat']; ?>"><span class="btleft"></span><span class="btmiddle">Annuler</span><span class="btright"></span></a></div>
		<div class="bt"><a href="../index.php?Numero_contrat=<?php echo $_SESSION['num_contrat']; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
		
		<div class="bt"><span class="btleft"></span><input type="submit" value="Valider" <?php if(retourValForm('id')==1) echo "disabled='disabled'"; ?> class="resizeall btmiddle" /><span class="btright"></span></div>


  <input type="hidden" name="Identifiant_garantie_souscrite" value="" />
  <input type="hidden" name="Etat_garantie_souscrite" value="" />
  <input type="hidden" name="Date_effet_garantie_souscrite" value="" />
  <input type="hidden" name="Date_fin_effet_garantie_souscrite" value="" />
  <input type="hidden" name="Identifiant_contrat" value="" />
  <input type="hidden" name="Identifiant_personne" value="" />
  <input type="hidden" name="Identifiant_formule" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
	</div>
	<div class="imgright"></div>
	<div class="cleared"></div>
</form>
</div>
</body>
</html>
<?php
mysql_free_result($rs_pers);
mysql_free_result($rsgarantie);
//unset $_SESSION['idpers'];
?>